package com.darren.archive.controller;

import com.darren.archive.entity.SysParameter;
import com.darren.archive.service.SysParameterService;
import com.darren.framework.bean.ResponseBean;
import com.darren.framework.controller.BaseController;
import com.darren.framework.utils.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value = "系统参数", tags = "系统参数接口", description = "系统参数接口")
@RestController
public class SysParameterController extends BaseController {
	
	@Autowired
	private SysParameterService sysParameterService;

	/**
	 * 根据 系统参数id 查询系统参数
	 * @param id  系统参数id
	 * @return    系统参数
	 */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "系统参数id", required = true, paramType = "path", dataType = "Long")
	})
	@ApiOperation(value = "根据 id 查询", notes = "根据 系统参数id 查询系统参数")
	@GetMapping("/sys/parameter/{id}")
	public ResponseBean getSystemParameterById(@PathVariable(name="id",required=true) Long  id){
		Element element = ehCacheManager.getCache(Constant.CACHE_SYSTEM_PARAMS).get(id);
		SysParameter sysParameter = null;
		if(element != null){
			sysParameter = (SysParameter) element.getObjectValue();
			logger.info("缓存获取！");
		}else{
			sysParameter = sysParameterService.findById(id);
			logger.info("数据库获取！");
		}
		return ResponseBean.isOk(sysParameter);
	}


	/**
	 * 查询所有系统参数
	 * @return  所有系统参数
	 */
	@ApiOperation(value = "查询所有系统参数", notes = "查询所有系统参数")
	@GetMapping("/sys/parameters/list")
	public ResponseBean getSystemParameters(HttpServletRequest request){
		Element element = ehCacheManager.getCache(Constant.CACHE_SYSTEM_PARAMS).get("AllParam");
		List<SysParameter> sysParameters = null;
		if(element != null){
			sysParameters = (List<SysParameter>)element.getObjectValue();
			logger.info("从缓存中获取到所有系统参数！");
		}else{
			sysParameters = sysParameterService.getAllSystemParameter();
			element = new Element("AllParam",sysParameters);
			ehCacheManager.getCache(Constant.CACHE_SYSTEM_PARAMS).put(element);
			logger.info("从数据库获取到所有系统参数！");
		}
		return ResponseBean.isOk(sysParameters);
	}
	

	
}
